package com.sh.data.engine.application.service.normalization;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.normalization.research.ThemeResponse;
import com.sh.data.engine.application.model.mapper.normalization.ThemeResponseMapper;
import com.sh.data.engine.application.model.mapper.normalization.ThemeResponsePageMapper;
import com.sh.data.engine.application.model.request.normalization.MoveThemeRequest;
import com.sh.data.engine.application.model.request.normalization.SaveThemeRequest;
import com.sh.data.engine.common.util.ServletUtils;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.normalization.research.themedesign.model.domain.ThemeDomain;
import com.sh.data.engine.domain.normalization.research.themedesign.model.param.ListThemeParam;
import com.sh.data.engine.domain.normalization.research.themedesign.model.param.MoveThemeParam;
import com.sh.data.engine.domain.normalization.research.themedesign.model.param.ThemeParam;
import com.sh.data.engine.domain.normalization.research.themedesign.service.ThemeService;
import com.sh.data.engine.domain.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: mengzheng.mhc
 * @date: 2024/7/3 9:43
 */
@Slf4j
@Service
public class ThemeApplicationService {

    @Autowired
    private ThemeService themeService;

    @Autowired
    private ThemeResponseMapper themeResponseMapper;

    @Autowired
    private ThemeResponsePageMapper themeResponsePageMapper;

    public boolean saveTheme(SaveThemeRequest themeRequest) {
        Long projectId = ServletUtils.getProjectId();
        themeService.saveTheme(ConvertUtil.copyProperties(themeRequest, ThemeParam.class), projectId);
        return true;
    }

    public boolean deleteTheme(Long themeId) {
        Long projectId = ServletUtils.getProjectId();
        themeService.deleteTheme(themeId, projectId);
        return true;
    }

    public PagerResponse<ThemeResponse> listThemes(Integer pageNum, Integer pageSize, String lastThemeId, Long pid) {
        Long projectId = ServletUtils.getProjectId();
        ListThemeParam listThemeParam =
            ListThemeParam.builder().pageNum(pageNum).pageSize(pageSize).lastThemeId(lastThemeId).pid(pid).build();
        PageResult<ThemeDomain> themeDomainPageResult = themeService.listThemes(listThemeParam, projectId);
        return themeResponsePageMapper.toPageResponse(themeDomainPageResult);
    }

    public List<ThemeResponse> treeThemes() {
        Long projectId = ServletUtils.getProjectId();
        List<ThemeDomain> themeDomains = themeService.getThemesByProjectId(projectId);
        return themeResponseMapper.map(themeDomains);
    }

    public boolean moveTheme(MoveThemeRequest moveThemeRequest) {
        Long projectId = ServletUtils.getProjectId();
        themeService.moveTheme(ConvertUtil.copyProperties(moveThemeRequest, MoveThemeParam.class), projectId);
        return true;
    }
}
